<template>
  <div class="home-content">
    <el-container>
      <el-header>
        <div class="header-logo">
          <img src="../assets/heima.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="quit">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isMenuCollapse ? '64px' : '200px'" >
          <!-- 导航菜单折叠 -->
          <div class="menu-fold" @click="menuFold">|||</div>
          <!--导航菜单 -->
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF"
            unique-opened
            :collapse="isMenuCollapse"
            collapse-transition
            router
            :default-active="isdefaultActive"
          >
            <el-submenu
              :index="item.id + ''"
              v-for="item in menuListData"
              :key="item.id"
            >
              <template slot="title">
                <i :class="oneMenuIcon[item.id]" class="oneMenuIcon"></i>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="activePath('/' + subItem.path)"
              >
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main><router-view></router-view></el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 菜单权限数据
      menuListData: [],
      // 控制左侧菜单折叠与展开
      isMenuCollapse: false,
      // 左侧一级菜单字体图标
      oneMenuIcon: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 当前高亮的效果
      isdefaultActive: ''
    }
  },
  created() {
    this.homeLeftMenuList()
    // 获取当前页面高亮的Path
    this.isdefaultActive = window.sessionStorage.getItem('activePath')
  },
  methods: {
    quit() {
      // 清空token
      window.sessionStorage.clear()
      // 跳转到登录页面
      this.$router.push('/login')
    },
    // 获取左侧菜单权限
    async homeLeftMenuList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      this.menuListData = res.data
    },
    // 导航菜单折叠
    menuFold() {
      this.isMenuCollapse = !this.isMenuCollapse
    },
    // 获取当前页面的Path并且高亮
    activePath(path) {
      // 将获取到当前页面的路径保存到浏览器缓存中
      window.sessionStorage.setItem('activePath', path)
      this.isdefaultActive = path
    }
  }
}
</script>

<style lang="less" scoped>
.home-content {
  height: 100%;
}
.el-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  .header-logo {
    display: flex;
    align-items: center;
    span {
      font-size: 20px;
      color: #fff;
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .menu-fold {
    background-color: #4a5064;
    text-align: center;
    color: #fff;
    padding: 3px 0;
    font-size: 12px;
    cursor: pointer;
    letter-spacing: 3px;
  }
}

.el-main {
  background-color: #eaedf1;
}

.el-menu {
  border-right: none;
}

.oneMenuIcon {
  padding-right: 10px;
}
</style>
